<template>
  <div class="list">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10" class="selectinput">
          <el-input
            placeholder="请输入内容"
            v-model="params.query"
            class="input-with-select"
            clearable
            @clear="clearquery"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="addGood">添加列表</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="80">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160">
          <template v-slot="scope">
            {{ scope.row.add_time | startTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editInfo(scope.row.goods_id)"
              >编辑</el-button
            >
            <!--  删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="isDelete(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pagenum"
        :page-sizes="[10, 15, 20]"
        :page-size="params.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import moment from "moment";
export default {
  name: "",
  components: {},
  data() {
    return {
      goodsList: [],
      params: {
        query: "",
        //页码
        pagenum: 1,
        //当前每页显示几条数据
        pagesize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$request({
        url: "goods",
        params: this.params,
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      console.log(res);
    },
    //页面条目发生变化
    handleSizeChange(newsize) {
      this.params.pagesize = newsize;
      this.getGoodsList();
    },
    //页码发生变化
    handleCurrentChange(newpage) {
      this.params.pagenum = newpage;
      this.getGoodsList();
    },
    clearquery() {
      this.getGoodsList();
    },
    async isDelete(id) {
      const res = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      if (res !== "confirm") {
        return this.$message({
          type: "info",
          message: "已取消删除",
        });
      }
      const { data: ress } = await this.$request({
        url: `goods/${id}`,
        method: "delete",
      });
      if (ress.meta.status !== 200) {
        return this.$message.error(ress.meta.msg);
      }
      this.getGoodsList();
      this.$message.success("删除成功");
    },
    addGood() {
      console.log("************");
      this.$router.push("addgood");
    },
  },
  filters: {
    startTime(val) {
      return moment(val * 1000).format("YYYY-MM-DD hh:mm:ss");
    },
  },
};
</script>
<style lang="scss" scoped>
.el-table {
  margin-top: 30px;
}
</style>
